package jbl.service.impl;

import java.util.ArrayList;
import java.util.List;

import jbl.dao.TuserGroupMenuDaoI;
import jbl.model.Code_tusergroup;
import jbl.model.T_usergroup_menu;
import jbl.model.Tmenu;
import jbl.pageModel.DataGrid;
import jbl.pageModel.UserGroupMenu;
import jbl.service.UserGroupMenuServiceI;
import jbl.util.Encrypt;
import jbl.util.Json;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * 用户权限菜单表
 * */
@Service("userGroupMenuService")
public class UserGroupMenuServiceImpl implements UserGroupMenuServiceI{
	
	private static final Logger logger = Logger.getLogger(UserGroupMenuServiceImpl.class);

	
	private TuserGroupMenuDaoI userGroupMenuDao;
	
	@Autowired
	public void setUserGroupMenuDao(TuserGroupMenuDaoI userGroupMenuDao) {
		this.userGroupMenuDao = userGroupMenuDao;
	}


	@Override
	public DataGrid dataGrid() {
		// TODO Auto-generated method stub
		DataGrid dg = new DataGrid();
		try {
			List<UserGroupMenu> rl = new ArrayList<UserGroupMenu>();
			String hql = "from T_usergroup_menu t ";
			List<T_usergroup_menu> nl = userGroupMenuDao.find(hql);
			if(null != nl && nl.size() > 0){
				for(T_usergroup_menu t : nl){
					UserGroupMenu userGroupMenu = new UserGroupMenu();
					BeanUtils.copyProperties(t, userGroupMenu);
					addWjData(t, userGroupMenu);
					rl.add(userGroupMenu);
				}
				dg.setTotal((long)rl.size());
				dg.setRows(rl);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("查询失败："+ e.getMessage());
		}
		return dg;
	}
	
	private void addWjData(T_usergroup_menu t,UserGroupMenu userGroupMenu){
		Tmenu tmenu = t.getTmenu();
		if(null != tmenu){
			List<Object> list = new ArrayList<Object>();
			list.add(tmenu.getText());
			list.add(tmenu.getId());
			userGroupMenu.setMenuList(list);
		}
		Code_tusergroup tusergroup = t.getTusergroup();
		if(null != tusergroup){
			List<Object> list = new ArrayList<Object>();
			list.add(tusergroup.getUserGroupName());
			list.add(tusergroup.getId());
			userGroupMenu.setUserGroupList(list);
		}
		
	}
	
	private void  addWj(T_usergroup_menu t,UserGroupMenu userGroupMenu){
		List<Object> menuList = userGroupMenu.getMenuList();
		if(null != menuList && menuList.size() > 0){
			Tmenu tmenu = new Tmenu();
			tmenu.setId(menuList.get(0).toString());
			t.setTmenu(tmenu);
		}
		List<Object> userGroupList = userGroupMenu.getUserGroupList();
		if(null != userGroupList && userGroupList.size() > 0){
			Code_tusergroup tusergroup = new Code_tusergroup();
			tusergroup.setId(userGroupList.get(0).toString());
			t.setTusergroup(tusergroup);
		}
	}


	@Override
	public Json add(UserGroupMenu userGroupMenu) {
		Json j = new Json();
		try {
			T_usergroup_menu t = null;
			String hql = " from T_usergroup_menu t where t.tmenu.id=? and t.tusergroup.id = ? ";
			t = userGroupMenuDao.get(hql, new Object[]{userGroupMenu.getMenuList().get(0).toString(), userGroupMenu.getUserGroupList().get(0).toString()});
			if(null != t){
				j.setMsg("已拥有该权限!");
				return j;
			}else{
				t = new T_usergroup_menu();
			}
			t.setId(Encrypt.createUUID());
			addWj(t, userGroupMenu);
			
			userGroupMenuDao.save(t);
			j.setMsg("添加成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("添加失败");
		}
		return j;
	}

	
	@Override
	public Json edit(UserGroupMenu userGroupMenu) {
		Json j = new Json();
		try {
			T_usergroup_menu t = null;
			String hql = " from T_usergroup_menu t where t.tmenu.id=? and t.tusergroup.id = ? ";
			t = userGroupMenuDao.get(hql, new Object[]{userGroupMenu.getMenuList().get(0).toString(), userGroupMenu.getUserGroupList().get(0).toString()});
			if(null != t){
				j.setMsg("已拥有该权限!");
				return j;
			}else{
				t = new T_usergroup_menu();
			}
			t.setId(userGroupMenu.getId());
			addWj(t, userGroupMenu);
			userGroupMenuDao.update(t);
			j.setMsg("修改成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("修改失败");
		}
		return j;
	}

	@Override
	public Json delete(String ids) {
		Json j = new Json();
		try {
			if(ids != null){
				for (String id : ids.split(",")) {
					if(!id.trim().equals("")){
						T_usergroup_menu t = userGroupMenuDao.get(T_usergroup_menu.class,id.trim());
						userGroupMenuDao.delete(t);
					}
				}
			}
			j.setMsg("删除成功");
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg("删除失败");
		}
		return j;
	}
}











